package com.mall.controller;

import com.mall.dto.ActionResult;
import com.mall.po.Discounts;
import com.mall.service.DiscountsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @author zhanghui
 * @ClassName DiscountsController
 * @Description TODO
 * @Date 2021/1/6 22:27
 * @Version 1.0
 **/
@Api(tags = "优惠相关操作")
@RestController
@RequestMapping("/shangcheng")
public class DiscountsController {
    @Autowired
    private DiscountsService discountsService;
    /**
     * @Author zhanghui
     * @Description 根据商品名查询活动信息
     * @Date 2021/1/6 17:24
     * @Param
     * @return
     **/
    @ApiOperation(value = "查询单个优惠名称")
    @ApiImplicitParam(name = "discountsname",value = "优惠商品名称",required = true,dataType = "String")
    @RequestMapping(value = "/discountquery",method = RequestMethod.GET)
    public ActionResult discountquery(String discountsname) {
        Discounts discounts = discountsService.findDiscountsByName(discountsname);
        if (discountsService.findDiscountsByName(discountsname) != null) {
            return new ActionResult(200,"查询成功",discounts);
        }
        return null;
    }
    /**
     * @Author zhanghui
     * @Description 添加优惠
     * @Date 2021/1/6 18:56
     * @Param
     * @return
     **/
    @ApiOperation(value = "添加优惠信息")
    @ApiImplicitParam(name = "discounts",value = "没有优惠id的对象（json）",required = true)
    @RequestMapping(value = "/discountadd",method = RequestMethod.POST)
    public ActionResult discountadd(@RequestBody Discounts discounts) {
        if (discountsService.addDiscounts(discounts) > -1) {
            return new ActionResult(200,"添加成功",null);
        }
        return null;
    }
    /**
     * @Author zhanghui
     * @Description 修改优惠
     * @Date 2021/1/6 19:42
     * @Param
     * @return
     **/
    @ApiOperation(value = "更新优惠内容")
    @ApiImplicitParam(name = "discounts",value = "更改后的内容",required = true)
    @RequestMapping(value = "/discountupdate",method = RequestMethod.PUT)
    public ActionResult discountupdate(@RequestBody Discounts discounts){
        if(discountsService.updateDiscounts(discounts)>-1){
            return new ActionResult(200,"修改成功",null);
        }
        return new ActionResult(400,"修改失败",null);
    }
    /**
     * @Author zhanghui
     * @Description 删除优惠
     * @Date 2021/1/6 19:51
     * @Param
     * @return
     **/
    @ApiOperation(value = "删除优惠")
    @ApiImplicitParam(name = "discountsid",value = "要删除目标优惠的id",required = true)
    @RequestMapping(value = "/discountdel",method = RequestMethod.DELETE)
    public ActionResult discountdel(int discountsid){
        if(discountsService.deleteDiscounts(discountsid)>-1){
            return new ActionResult(200,"删除成功",null);
        }
        return new ActionResult(400,"删除失败",null);
    }
    /**
     * @Author zhanghui
     * @Description 批量删除
     * @Date 2021/1/7 15:48
     * @Param [discountsids]
     * @return com.mall.dto.ActionResult
     **/
    @ApiOperation(value = "批量删除优惠")
    @ApiImplicitParam(name = "discountsids",value = "要删除的优惠id数组",required = true)
    @RequestMapping(value = "/discountsdel",method = RequestMethod.DELETE)
    public ActionResult discountsdel(Integer[] discountsids){
        if(discountsService.deleteDiscountList(discountsids)){
            return new ActionResult(200,"批量删除成功",null);
        }
        return new ActionResult(400,"批量删除失败",null);
    }

    /**
     * @Author zhanghui
     * @Description 分页查询
     * @Date 2021/1/7 16:27
     * @Param [page, pagesize]
     * @return java.util.List<com.mall.po.Discounts>
     **/
    @ApiOperation(value = "分页获取优惠信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "查询的页码",required = true ),
            @ApiImplicitParam(name = "pagesize",value = "每一页包含数据条数",required = true)
    })
    @RequestMapping(value = "/discountsqueryBypage",method = RequestMethod.GET)
    public List<Discounts> discountsqueryBypage(int page,int pagesize){
        return discountsService.findDiscountsByPage(page,pagesize);
    }

}
